package com.mm.fam.node.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
@Component
public class Config implements BeanPostProcessor{
    public final static Map<String, String> MAP_CONFIG = new HashMap<>();
    private Logger logger = LoggerFactory.getLogger(Config.class);

    @Value("${rt.rpc.client.node-id}")
    private int nodeId;
    @Value("${rt.rpc.client.token}")
    private String token;
    @Value("${scop}")
    private String scop;


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        if(Config.MAP_CONFIG.size() > 0){
            return bean;
        }
        logger.info("beanName:"+beanName);
        logger.info("---------------开始初始化------------------");
        if("pro".equals(scop)){
            logger.info("生产配置");
            logger.info("nodeId:"+System.getenv("nodeId"));
            MAP_CONFIG.put("nodeId",System.getenv("nodeId"));
            logger.info("token:"+System.getenv("token"));
            MAP_CONFIG.put("token",System.getenv("token"));
        }else{
            logger.info("开发配置");
            logger.info("nodeId:"+nodeId);
            MAP_CONFIG.put("nodeId",Integer.toString(nodeId));
            logger.info("token:"+token);
            MAP_CONFIG.put("token",token);
        }
        logger.info("---------------结束初始化------------------");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

}

